package com.wangxinenpu.springbootdemo.service.impl;


import com.wangxinenpu.springbootdemo.config.JwtTokenUtil;
import com.wangxinenpu.springbootdemo.config.security.XlResource;
import com.wangxinenpu.springbootdemo.dao.mapper.SysUserMapper;
import com.wangxinenpu.springbootdemo.dataobject.model.SysUser;
import com.wangxinenpu.springbootdemo.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户 业务层处理
 * 
 * @author ruoyi
 */
@Service
@Slf4j
public class SysUserServiceImpl implements ISysUserService
{

    @Resource
    public SysUserMapper sysUserMapper;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Resource
    private PasswordEncoder passwordEncoder;

//    @Resource
//    private UserDetailsService userDetailsService;

    @Override
    public SysUser selectUserByUserName(String username) {
        return sysUserMapper.selectUserByUserName(username);
    }

    @Override
    public List<XlResource> getResourceList(Long userId) {
        return new ArrayList<>();
    }

//    @Override
//    public String login(String username, String password) {
//        String token = null;
//        try {
//            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
//            if (!passwordEncoder.matches(password, userDetails.getPassword())) {
//                throw new BadCredentialsException("密码不正确");
//            }
//            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
//            SecurityContextHolder.getContext().setAuthentication(authentication);
//            token = jwtTokenUtil.generateToken(userDetails);
//        } catch (AuthenticationException e) {
//            log.warn("登录异常:{}", e.getMessage());
//        }
//        return token;
//
//    }

}
